﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Base3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Init();

            // 禁止自动生成列
            this.dgvEList.AutoGenerateColumns = false;
            // 禁止用户鼠标调整列 行 列头的宽度
            this.dgvEList.AllowUserToResizeColumns = false;
            this.dgvEList.AllowUserToResizeRows = false;
            this.dgvEList.ColumnHeadersHeightSizeMode= DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
        }

        private void Init()
        {
            // 定义职位下拉框的选项。 创建多个Pos对象，并将其存放在List中
            List<Pos> posList = new List<Pos>()
            {
                new Pos(){PosId=1,PosName="软件工程师"},
                new Pos(){PosId=2,PosName="锅炉工"},
                new Pos(){PosId=3,PosName="门卫"},
                new Pos(){PosId=4,PosName="运维工程师"}
            };

            List<Department> depList = new List<Department>()
            {
                new Department(){DepartmentId=1,DepartmentName="开发部"},
                new Department(){DepartmentId=2,DepartmentName="后勤部"},
                new Department(){DepartmentId=3,DepartmentName="管理部"}
            };

            this.cbbPos.DataSource = posList;           // 绑定数据源
            this.cbbPos.DisplayMember = "PosName";      // 下拉选项显示 Pos对象的PosName属性
            this.cbbPos.ValueMember = "PosId";          // 但在存储时 存储的是对应的PosId
            this.cbbPos.SelectedIndex = -1;             // 不存在默认选项

            this.cbbDepartment.DataSource = depList;
            this.cbbDepartment.DisplayMember = "DepartmentName";
            this.cbbDepartment.ValueMember = "DepartmentId";
            this.cbbDepartment.SelectedIndex = -1;
        }

        private void btnShowData_Click(object sender, EventArgs e)
        {
            // 测试dataGrideView显示
            List<Employee> eList = new List<Employee>();
            for(int i=1;i<=9;i++)
            {
                eList.Add(new Employee()
                {
                    EmployeeName = "员工" + i,
                    IdCard = "15646023100"+i,
                    Tel="182651300"+i,
                    Pos="职位"+i,
                    Department="部门"+i,
                    Gender=(i % 2 == 0) ?"男":"女",
                    Province="地区"+i
                });
            }

            // 将dataGrideView的数据源指定为 对应的List
            this.dgvEList.DataSource = eList;
        }

        private void btnSaveData_Click(object sender, EventArgs e)
        {
            // 测试ComboBox
            if(this.cbbPos.SelectedIndex==-1)
            {
                MessageBox.Show("请选择一个职位");
                return;
            }
            MessageBox.Show(this.cbbPos.Text);
            MessageBox.Show(this.cbbPos.SelectedValue.ToString());
        }

        private void llblPhoto_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();   //创建一个打开文件对话框对象
            DialogResult dialogResult = openFileDialog.ShowDialog();    // 并以模态方式展示该对象
            if(dialogResult==DialogResult.OK)   // 成功打开后
            {
                string path = openFileDialog.FileName;  // 打开的文件的路径
                string extName = path.Substring(path.LastIndexOf(".") + 1); // 获取文件后缀
                if(extName=="jpg"||extName=="png")
                {
                    this.pb_Photo.Image = Image.FromFile(path);
                }
                else
                {
                    MessageBox.Show("图片的格式必须为jpg或png！","提示信息");
                }

            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            // 点击关闭时 弹出
            // 参数1：要在消息框中显示的文本  参数2：要在消息框的标题栏中显示的文本
            // 参数3：可指定在消息框中显示哪些按钮   参数4：指定在消息框中显示哪个图标
            DialogResult dialogResult = MessageBox.Show("确定退出当前程序吗？","退出确认", 
                                                        MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if(dialogResult==DialogResult.Cancel)
            {
                e.Cancel = true;  // 取消事件
            }
        }

        //按钮切换图标和逻辑功能
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //首先判断状态
            if(this.btnAdd.Tag.ToString()=="1") // 1为保存逻辑
            {
                // 编写保存逻辑....

                // 保存逻辑执行完后 切换为修改逻辑
                this.btnAdd.ImageIndex = 1;     // 图标的切换
                this.btnAdd.Text = "  修改数据";
                this.btnAdd.Tag = 0;
            }
            else
            {
                // 编写修改逻辑....

                // 修改逻辑执行完后 切换为保存逻辑
                this.btnAdd.ImageIndex = 3;
                this.btnAdd.Text = "  保存数据";
                this.btnAdd.Tag = 1;
            }

        }

        private void btnSwitch_Click(object sender, EventArgs e)
        {
            FListView fListView = new FListView();
            fListView.Show();
        }
    }
}
